package com.game.raceroom
{
	import com.game.chat.ChatCanvas;
	import com.game.main.MainCanvas;
	import com.game.raceroom.HintCanvas;
	import com.game.raceroom.events.GameEvent;
	import com.game.raceroom.player.Player;
	import com.game.raceroom.player.contrl.ActorContrl;
	import com.game.raceroom.player.contrl.KeyboardContrl;
	import com.game.raceroom.player.effect.ActionChecker;
	import com.game.raceroom.player.effect.HintItemEffect;
	import com.game.raceroom.player.effect.skill.JumpEffect;
	import com.game.raceroom.player.skill.Skill;
	import com.util.DEF;
	import com.util.Vector;
	
	import flash.display.Stage;
	import flash.utils.clearInterval;
	import flash.utils.getTimer;
	import flash.utils.setInterval;
	
	import mx.containers.Canvas;
	
	
	public class TyroRoom extends Room
	{
		 
		public  function TyroRoom(id:int,GC:Canvas,stage:Stage,chat:ChatCanvas,_parents:Object)
		{
			 super(id,GC,stage,chat,_parents);
		
		}
		public override function startRacing():void{
			if(!isRunning){
				 isRunning=true;
				 run_id=setInterval(run,DEF.FRAME_RATE);
   			} 		
		}
		public override function stopRacing():void{
			if(isRunning){
			   isRunning=false;
			   clearInterval(run_id);
			}  
		}
 
	    public override function run():void{
        	if(started){
        		currentSystemTime=getTimer();
        		if(!lockKeyboard)
        		checkKeyboardDirInput();
	        	showHint();
	        	
	        	updatePlayers();
	        	mapContrl.updateMap();
	        	showPlayers();
	        	updateHincMc();
	        	//locked=false;
	        	checkEffects();
	        	skillBar.updateSkills();
        	}
        	//checkCrash();
        }
   


	
	    public override function sendAction():void{

        }
 		public  override function onSkill(e:GameEvent):void
		{
        	if(!started)return;
        	//if(waitingAction)return;
        	//waitingAction=true;
        	sendSkill();
		}

	    public override function sendSkill():void{
			var p:Player = actorContrl;
			switch(tempSkill){
				case Skill.SKILL_TYPE_STRICK:
	 		        lastInputTime=currentSystemTime;
        	        actorContrl.keyInput.reset();
	 			    ac.spellSkillStrick(p,p._x,p._y,p._angle);
	 			    itemCount++;
				break;
				case Skill.SKILL_TYPE_JUMP:
				     var eft:JumpEffect=new JumpEffect(p);
   		            addEffect(eft);	
   		            itemCount++;
				break;
			}
			if(itemCount>=5)this.changeHint();
			tempSkill=-1;
			
        }

  
        public override function initMapOk():void{
        	trace("start login game server");
        	//mync.call("userLogin",null,actorInfo.userID,roomid,this.lIP,this.lPort);
               acCanvas.visible=true;
               bgCanvas.visible=true;
               topCanvas.visible=true;
               fgCanvas.visible=true;	
        }
        private var hintStatus:int=0;
        private var lockKeyboard:Boolean=true;
        private var hintBar:HintCanvas=new HintCanvas;
        private var hintChange:Boolean=true;
        public function showHint():void{
        	if(!hintChange)return;
        	hintChange=false;
        	switch(hintStatus){
        		case 0:
	        	    hintBar.x=80;
				    hintBar.y=100;
				    this.skillBar.lock=true;
				    this.skillBar.visible=false;
	        		this.topCanvas.addChild(hintBar);
	        		hintBar.room=this;
	        		hintBar.alpha=0.5;
	        		hintBar.mainCanvas=this._parents.mainCanvas;
	        		hintBar.hint.text="欢迎来到训练场，在这里你将学习到车辆的基本操作方法以及部分道具的用法。";
	        		break;
        		case 1:
        			initHint1();
        			hintBar.alpha=0.2;
                    lockKeyboard=false;
	        		hintBar.hint.text="第1课 前进：按住' W '或者' ↑ '键，将车开到前方闪亮光柱处.";
	        		break;
        		case 2:
                    lockKeyboard=true;
                    this.actorContrl._x=540;
                    this.actorContrl._y=800;
                    actorContrl.up_speed=0;
                    actorContrl.down_speed=0;
                    actorContrl._speed=0;
                    hintBar.alpha=0.5;
                    actorContrl.sideClipContrl.speed=0;
                    actorContrl.resetAction();
                    this.mapContrl.updateMap();
	        		hintBar.hint.text="干的漂亮！点击下一步继续学习新内容";
	        		hintBar.resume();
	        		break;
        		case 3:
                    initHint2();
                    hintBar.alpha=0.2;
                    lockKeyboard=false;
                    hintBar.hint.text="第2课  转向：车辆开动过程中，按住'A'或'←'键向左转，按住'D'或'→'键向右转,请碰撞所有光柱!";
	        		break;
	        	case 4:
                     
                    lockKeyboard=true;
                    this.actorContrl._x=540;
                    this.actorContrl._y=800;
                    actorContrl.up_speed=0;
                    actorContrl.down_speed=0;
                    actorContrl._speed=0;
                    hintBar.alpha=0.5;
                    actorContrl.sideClipContrl.speed=0;
                    actorContrl.resetAction();
                    this.mapContrl.updateMap();
                    hintBar.hint.text="干的漂亮！点击下一步继续学习新内容";
                    hintBar.resume();
	        		break;
	        	case 5:
                     
                    hintBar.alpha=0.2;
                    this.skillBar.lock=false;
				    this.skillBar.visible=true;
				     lockKeyboard=false;
                    hintBar.hint.text="第3课  道具的使用：利用数字快捷键使用道具，或用鼠标点击道具图标使用道具。请使用5次道具。";
	        		break;
	        	case 6:
                     
                    hintBar.alpha=0.5;
                    
                    hintBar.hint.text="恭喜你毕业了！ 可以继续练习或者点击离开按钮返回碰碰村！";
	        		break;
        	}
        	
        	
        }
        private var itemCount:int=0;
        public function changeHint():void{
        	hintStatus++;
        	hintChange=true;
        } 
        private function initHint1():void{
        	 var mc:HitMC=new HitMC;
        	
        	var x:int=this.actorContrl._x;
        	var y:int= this.actorContrl._y-200;;
        	 var hintItem:HintItemEffect=new HintItemEffect(this,x,y,mc)
        	hints.put(hintItem);
        	
        }
        private function initHint2():void{
        	 var mc:HitMC=new HitMC;
        	 var x:int=this.actorContrl._x+200;
        	 var y:int= this.actorContrl._y;
        	 var hintItem:HintItemEffect=new HintItemEffect(this,x,y,mc)
        	 hints.put(hintItem);
        	 
        	 mc=new HitMC;
        	 x=this.actorContrl._x-200;
        	 y= this.actorContrl._y;
        	 hintItem=new HintItemEffect(this,x,y,mc)
        	 hints.put(hintItem);
        	
        }
        private var hints:Vector=new Vector;
        private var x_dis:Number;
        private var y_dis:Number;
        private var dis:Number;
        public function updateHincMc():void{
        	if(hints.size()>0){
	        	for(var i:int=0;i<hints.size();i++){
	        		var hint:HintItemEffect=(HintItemEffect)(hints.getObjById(i));
	        		hint.update();
	        	    x_dis=hint.x-this.actorContrl._x;
	        		y_dis=hint.y-this.actorContrl._y;
	        		x_dis=Math.abs(x_dis);
	        		y_dis=Math.abs(y_dis);
	        		dis=Math.sqrt(x_dis*x_dis+y_dis*y_dis);
	        		if(dis<=this.actorContrl.carInfo.confRadius){
	        			hint.destroy();
	        			hints.remove(i);
			        	if(hints.size()<=0){
			        		changeHint();
			        	}
	        			return;
	        		}
	        	}

        	}
        }
 

		public override function initSelf(x:int,y:int,ang:int,now:int,team:int):void{
			//actorInfo.currentCar=DEF.getTestCar1(new ActorDisplayer(this.acCanvas));
		  
		    actorContrl=new ActorContrl(acCanvas,actorInfo,mapContrl.mapInfo,this);
 		    actorContrl._x=x;
        	actorContrl._y=y;
            actorContrl._viewx=x;
		    actorContrl._viewy=y;
        	actorContrl._angle=ang;
        	//actorContrl.turnActor();
        	actorContrl.setReady(true);
 			actorContrl._parent = this;
			keyContrl=new KeyboardContrl(null);
			keyContrl.setGameCanvasKeyboardEvent(gameCanvas,actorContrl,this.skillBar);
			ac=new ActionChecker(this,this.actorContrl,this.players); 			
			players.put(actorContrl.userInfo.userNickName,actorContrl);
			skillBar.initSkills(this,actorContrl);
        	mapContrl.actorContrl=actorContrl;
        	actorContrl.mapContrl=mapContrl;
        	actorContrl.refBlood(now);
        	actorContrl.teamId=team;
 			mapContrl.updateMap();
 			started=true;
		}
 }
}